﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Najak;

namespace Falon
{
	public enum EncumbranceRating
	{
		None,
		Light,
		Medium,
		Heavy,
		Massive
	}
	public class Item : IAnimated
	{
		public EncumbranceRating Encumbrance;

		private bool _wasContentLoaded;
		protected bool _wasInitialized { get; private set; }

		public string TextureName;
		public Texture2D Texture;

		public string Name;

		public Vector2 BaseOffset;
		public Vector2 Offset;
		public float BaseRotation;
		public float Rotation;

		public Unit Owner;
		public Color Color = Color.White;
		public Vector2 Origin;

		public void SetFrame(Frame frame)
		{
			Rotation = BaseRotation + frame.Rotation;
			Offset = BaseOffset + frame.Offset;
		}

		public void SetOwner(Unit owner)
		{
			Owner = owner;
			if (Owner.Scene != null)
			{
				OnLoadContent();
			}

			BaseOffset = new Vector2(-12, 10);
			Offset = BaseOffset;
			Rotation = 0.0f;
			BaseRotation = Rotation;
		}

		public void Update(float timeDelta)
		{
			OnUpdate(timeDelta);
		}
		virtual protected void OnUpdate(float timeDelta)
		{
		}

		public virtual void Draw(SpriteBatch spriteBatch, Vector2 offset)
		{
			Vector2 pos = GetLocation(Vector2.Zero); 	//new Vector2(Owner.Location.X + x, Owner.Location.Y + y);
			if (Texture != null)
			{
				if (Owner.Scale == 1)
				{
					spriteBatch.Draw(Texture, pos + offset, null, Color, Owner.WeaponRotation + Rotation, Origin, 1.0f, SpriteEffects.None, 0.0f); ;
				}
				else
				{
					spriteBatch.Draw(Texture, pos + offset, null, Color, Owner.WeaponRotation + Rotation, Origin, Owner.Scale, SpriteEffects.None, 0.0f); ;
				}
			}
		}

		public Vector2 GetLocation(Vector2 animationOffset)
		{
			Vector2 offset2 = Offset + animationOffset;
			Vector3 offset = new Vector3(offset2, 0);
			Quaternion rot = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, Owner.WeaponRotation);
			offset = Vector3.Transform(offset, rot);
			offset *= -Owner.Scale;
			return Owner.Location + new Vector2(offset.X, offset.Y);
		}

		public virtual void OnLoadContent()
		{
			if (Texture == null && TextureName != null)
			{
				Texture = Owner.Scene.Content.Load<Texture2D>(TextureName);
			}
			if (Texture != null)
				Origin = new Vector2(Texture.Width / 2.0f, Texture.Height / 2.0f);
		}

		protected void UnloadContent()
		{
			if (!_wasContentLoaded)
				return; // nothing to unload

			try
			{
				OnUnloadContent();
			}
			finally
			{
				_wasContentLoaded = false;
			}
		}
		protected virtual void OnUnloadContent()
		{
			Texture = null;
			Origin = Vector2.Zero;
		}

		protected virtual void OnInitialize() { }
		protected virtual void OnInitializing() { }
		protected virtual void OnInitialized() { }

		public void EnsureInitialized()
		{
			if (_wasInitialized)
				return; // already constructed

			try
			{
				OnInitializing();
				OnInitialize();
				OnInitialized();
			}
			finally
			{
				_wasInitialized = true;
			}

		}
		public virtual Item Clone()
		{
			EnsureInitialized();
			Item clone = this.MemberwiseClone() as Item;
			return clone;
		}
	}
}
